﻿using System;
using System.IO;
using AppCommon;
using Controller;
using Model;

public partial class admincenter_AddPost : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {        
        if (!IsPostBack)
        {
            if (Session[Constants.S_ADMIN_USER] != null)
            {
                // Ok
            }
            else
            {
                Response.Redirect("~/admincenter/");
            }

            BindDatatoDDL.BindCategory(ddlCategory);
            // BindData();
            tbId.Text = AutoGenIdPost();
        }
    }

    protected void btnThemvao_OnClick(object sender, EventArgs e)
    {
        try
        {
            var post = new Post();
            var userSs = (User) Session[Constants.S_ADMIN_USER];
            post.Id = tbId.Text;
            post.UniqueId = Common.ConvertToUnSign(tbTieude.Text);
            post.IdUser = userSs.Id;
            post.IdCategory = Request.Form[ddlCategory.UniqueID]; // ddlChuyenmuc.SelectedValue.ToString();
            post.Title = tbTieude.Text.Replace("'", "''");
            post.Description = tbMota.Text.Replace("'", "''");
            post.Content = Server.HtmlEncode(ckeditor1.Text);            
            // Kiem tra tinh hop le cua hinh anh
            if (fuAnhbia.HasFile)
            {
                var pathAnhbia = Server.MapPath(Constants.UPLOAD_IPost);
                if (!Directory.Exists(pathAnhbia))
                {
                    Directory.CreateDirectory(pathAnhbia);
                }
                // validate file image
                string[] validFileTypes = {"gif", "png", "jpg", "jpeg"};
                var extFile = Path.GetExtension(fuAnhbia.PostedFile.FileName);                
                var isValidate = false;

                if (extFile != null)
                {
                    foreach (var type in validFileTypes)
                    {
                        if (extFile.Equals("." + type))
                        {
                            isValidate = true;
                            break;
                        }
                    }
                }

                if (!isValidate)
                {
                    lbValidateAb.ForeColor = System.Drawing.Color.Red;
                    lbValidateAb.Text = "Invalid file with extension!";
                    post.ImagePanorama = Constants.NO_IMAGE;
                }
                else
                {
                    lbValidateAb.ForeColor = System.Drawing.Color.Green;
                    lbValidateAb.Text = "Ok!";
                    // Luu file 
                    fuAnhbia.SaveAs(pathAnhbia + tbId.Text + "_pan_" + DateTime.Now.ToString("ddMMyyhhmmss") +
                                    extFile);
                    post.ImagePanorama = tbId.Text + "_pan_" + DateTime.Now.ToString("ddMMyyhhmmss") + extFile;
                }
            }
            else
            {
                post.ImagePanorama = Constants.NO_IMAGE;
            }
            // post.ImagePanorama = fuAnhbia.FileName;
            // post.ImageThumbnail = fuAnhDaidien.FileName;
            if (fuAnhDaidien.HasFile)
            {
                var pathAnhbia = Server.MapPath(Constants.UPLOAD_IPost);
                if (!Directory.Exists(pathAnhbia))
                {
                    Directory.CreateDirectory(pathAnhbia);
                }
                // validate file image
                string[] validFileTypes = { "gif", "png", "jpg", "jpeg" };
                var extFile = Path.GetExtension(fuAnhDaidien.PostedFile.FileName);                
                var isValidate = false;

                if (extFile != null)
                {
                    foreach (var type in validFileTypes)
                    {
                        if (extFile.Equals("." + type))
                        {
                            isValidate = true;
                            break;
                        }
                    }
                }

                if (!isValidate)
                {
                    lbValidateTb.ForeColor = System.Drawing.Color.Red;
                    lbValidateTb.Text = "Invalid file with extension!";
                    post.ImageThumbnail = Constants.NO_IMAGE;
                }
                else
                {
                    lbValidateTb.ForeColor = System.Drawing.Color.Green;
                    lbValidateTb.Text = "Ok!";
                    // Luu file 
                    fuAnhDaidien.SaveAs(pathAnhbia + tbId.Text + "_thum_" + DateTime.Now.ToString("ddMMyyhhmmss") +
                                    extFile);
                    post.ImageThumbnail = tbId.Text + "_thum_" + DateTime.Now.ToString("ddMMyyhhmmss") + extFile;
                }
            }
            else
            {
                post.ImageThumbnail = Constants.NO_IMAGE;
            }

            //
            post.Source = tbNguon.Text;
            post.LinkDownLoad = tbLinkDown.Text;
            post.Tags = tbTags.Text;
            post.LastUpdatedBy = userSs.UserName;
            post.FeedBack = tbPhanhoi.Text;
            post.Status = Convert.ToInt32(cbTrangThai.Checked);

            var postController = new PostController();
            var tmp = postController.AddPost(post);
            if (tmp > 0)
            {
                litResult.Text = "<p class=\"alert alert-success\">Thêm dữ liệu thành công.</p>";
            }
            else
            {
                litResult.Text = "<p class=\"alert alert-danger\">Thêm dữ liệu thất bại! Xảy ra lỗi trong quá trình cập nhật.</p>";
            }
            // reset text
            ResetInput();
        }
        catch (Exception)
        {
            
            throw;
        }
        
    }

    public void BindData()
    {
        var categoryController = new CategoryController();
        ddlCategory.DataSource = categoryController.GetListCategory();
        ddlCategory.DataTextField = "Name";
        ddlCategory.DataValueField = "Id";
        ddlCategory.DataBind();
    }
  
    public static string AutoGenIdPost()
    {
        return "fx" + Common.ConvertDateTimetoString();
    }

    public void ResetInput()
    {
        tbId.Text = AutoGenIdPost();
        tbMota.Text = "";
        ckeditor1.Text = "";
        tbTieude.Text = "";
        tbTags.Text = "";
        tbLinkDown.Text = "";
        tbNguon.Text = "";
        tbPhanhoi.Text = "";
    }

    
}